#include<iostream>
#include<pthread.h> // 使用linux的线程
#include<unistd.h>
#include<string>
#include<cstdio>
#include <sys/types.h>
using namespace std;
string ToHex(pthread_t tid)
{
    char id [64];
    snprintf(id,sizeof(id),"0x%lx",tid);
    return string(id);
}

void* newThread(void* ThreadName)
{
    cout<< "I am New Thread, MyName is :"<<(char*)ThreadName<<endl;
    int cnt=3;
    while(cnt--){
        sleep(1);
        
        cout<<"i am a child,child pid:"<<getpid()<<"pthead_id"<<ToHex(pthread_self())<<endl;
    }
    //return (void*)123;    
    pthread_exit((void*)123);
}
int main()
{
    pthread_t threadId; // 
    int ret  = pthread_create(&threadId,nullptr,newThread,(void*)"Thread-1"); //第一个参数是输出型参数

    cout<<"i am main thread,father pid:"<<getpid()<<endl;
    sleep(1);
    cout<<"ret"<<pthread_cancel(threadId)<<endl;
    void* retValue = nullptr;
    pthread_join(threadId,&retValue); // 等待新线程.
    cout<<"New Thread's exit code:"<<(long long)retValue<<endl;
    cout<<"main pthread exit,wait new thread success!"<<endl;
    return 0;
}